package com.lianzai.user.service.impl;

import com.lianzai.common.utils.R;
import com.lianzai.mobile.dao.WxUserDao;
import com.lianzai.mobile.domain.WxUserDO;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.lianzai.user.dao.UserInfoDao;
import com.lianzai.user.domain.UserInfoDO;
import com.lianzai.user.service.UserInfoService;



@Service
public class UserInfoServiceImpl implements UserInfoService {
	@Autowired
	private UserInfoDao userInfoDao;

	@Autowired
	private WxUserDao wxUserDao;
	
	@Override
	public UserInfoDO get(Long id){
		return userInfoDao.get(id);
	}

	@Override
	public UserInfoDO getByWxOpenId(String openId) {
		return userInfoDao.getByWxOpenId(openId);
	}
	
	@Override
	public List<UserInfoDO> list(Map<String, Object> map){
		return userInfoDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return userInfoDao.count(map);
	}
	
	@Override
	public int save(UserInfoDO userInfo){
		return userInfoDao.save(userInfo);
	}
	
	@Override
	public int update(UserInfoDO userInfo){
		return userInfoDao.update(userInfo);
	}
	
	@Override
	public int remove(Long id){
		return userInfoDao.remove(id);
	}

	@Override
	public int enable(Long id,Long status){
		Map<String, Object> map = new HashedMap();
		map.put("id",id);
		map.put("status",status);
		return userInfoDao.enable(map);
	}

	@Override
	public int batchRemove(Long[] ids){
		return userInfoDao.batchRemove(ids);
	}

	@Override
	public R registerWxUser(String mobile, String openid){
		UserInfoDO userInfo = new UserInfoDO();
		userInfo.setMobile(mobile);
		int save = userInfoDao.save(userInfo);
		if(save <= 0){
			return R.error("保存用户失败");
		}

		WxUserDO wxUser = wxUserDao.getByOpenId(openid);

		if(wxUser == null){
			return R.error("请先关注公众号");
		}

		wxUser.setUserId(userInfo.getId());
		int update = wxUserDao.update(wxUser);
		if(update <= 0){
			return R.error("更新微信公众号信息失败");
		}

		return R.ok();
	}
	
}
